import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAIStore = defineStore('ai', () => {
    // 浮窗状态
    const isWindowVisible = ref(false)
    const isMinimized = ref(false)

    // AI模型设置
    const currentAIModel = ref('') // 'tongyi' | 'dify'
    const aiModels = ref([
        { id: 'tongyi', name: '通义千问', icon: 'qwen-icon' },
        { id: 'dify', name: 'Dify本地模型', icon: 'dify-icon' },
        { id: 'yuanbao', name: '腾讯元宝', icon: 'yuanbao-icon' }
    ])

    // 对话状态
    const chatHistory = ref([])
    const isLoading = ref(false)
    const userInput = ref('')

    // 浮窗位置
    const windowPosition = ref({ x: 30, y: window.innerHeight - 500 })

    // 操作方法
    const toggleWindow = () => {
        isWindowVisible.value = !isWindowVisible.value
        if (isWindowVisible.value) isMinimized.value = false
    }

    const minimizeWindow = () => {
        isMinimized.value = true
    }

    const maximizeWindow = () => {
        isMinimized.value = false
    }

    const setAIModel = (modelId) => {
        currentAIModel.value = modelId
        // 切换模型时清空历史（可选）
        clearHistory()
    }

    const addMessage = (message) => {
        chatHistory.value.push(message)
    }

    const clearHistory = () => {
        chatHistory.value = []
    }

    const updateWindowPosition = (position) => {
        windowPosition.value = position
    }

    return {
        isWindowVisible,
        isMinimized,
        currentAIModel,
        aiModels,
        chatHistory,
        isLoading,
        userInput,
        windowPosition,
        toggleWindow,
        minimizeWindow,
        maximizeWindow,
        setAIModel,
        addMessage,
        clearHistory,
        updateWindowPosition
    }
})